home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / ACL / CCL Folder / Examples / b.trees / Tree.cp next >
Encoding:
Text File  |  1994-08-31  |  3.2 KB  |  163 lines  |  [TEXT/MMCC]

  1. /********************************************
  2.  **** Core Class Demo by Yves Schmid
  3.  ****
  4.  **** Tree.cp
  5.  ****
  6.  **** Author:        Yves Schmid  
  7.  **** Created:      14 August 1994
  8.  **** Modified:     14 August 1994
  9.  **** Version:      1.0
  10.  **** Compatible:   C++
  11.  ****
  12.  **** Description:  Tree structure demo
  13.  ****
  14.  *************************/
  15.  
  16.  
  17. #include "CoreHead.h"
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. //*************************************
  23.  
  24.  
  25. const long cmd_printmyself    = 1024;    // A command which asks the stringnode to print its string
  26.  
  27.  
  28. //*************************************
  29. // A simple CoreHead which stocks and prints a string
  30.  
  31. class stringnode : public CoreHead
  32. {
  33.     char    data[256];
  34.  
  35.  
  36.     protected:
  37.  
  38.  
  39.     // Override the "receivecmd" to handle the "cmd_printmyself" command:
  40.  
  41.     void receivecmd(long cmd, void *info);
  42.  
  43.     public:
  44.  
  45.     // Constructors
  46.  
  47.     stringnode(char *str, CoreHead *supervisor);    // Object with a supervisor
  48.     stringnode(char *str);                            // Unlinked object
  49.     
  50.     // Destructor
  51.     
  52.     ~stringnode();
  53. };
  54.  
  55. //..............................
  56.  
  57. stringnode::stringnode(char *str, CoreHead *supervisor) : CoreHead(supervisor,0,1)    
  58.                                             // Initialize CoreHead with the supervisor,
  59.                                             // destination entry 0 and 1 entry in the new
  60.                                             // created object 
  61.                                              
  62. {
  63.     newlevel();            // A new heritage level!
  64.     strcpy(data,str);    // Copy string
  65. }
  66.  
  67. //..............................
  68.  
  69.  
  70. stringnode::stringnode(char *str) : CoreHead(1)    // Unlinked object with one entry 
  71. {
  72.     newlevel();            // A new heritage level!
  73.     strcpy(data,str);    // Copy string
  74. }
  75.  
  76. //..............................
  77.  
  78. stringnode::~stringnode()
  79. {
  80.     printf("%s deleted\n", data);
  81. }
  82.  
  83. //..............................
  84.  
  85.  
  86. void stringnode::receivecmd(long cmd, void *info)
  87. {
  88.     switch(cmd)
  89.     {
  90.         case cmd_printmyself:    // Ok it is the right message, I can print the string.
  91.         printf("%s\n",data);    
  92.         break;
  93.         
  94.         default:                // Unknow message? Call the parent class
  95.         CoreHead::receivecmd(cmd,info);
  96.     }
  97. }
  98.  
  99. //..............................
  100.  
  101.  
  102. //*******************************************************************
  103.  
  104.  
  105. void main()
  106. {
  107.     // Creating a tree:
  108.     //
  109.     //          A
  110.     //        /   \
  111.     //       B1     B2
  112.     //      / \    / \
  113.     //     C1 C2 D1  D2
  114.  
  115.     printf("*** CCL Tree demo ***\n\n");
  116.     printf("Creating a tree:\n\n");
  117.     printf("         A            \n");
  118.     printf("       /   \\        \n");
  119.     printf("      B1    B2        \n");
  120.     printf("     /\\     /\\    \n");
  121.     printf("    C1 C2  D1 D2    \n");
  122.  
  123.     //*****************************
  124.  
  125.     stringnode *a,*b1,*b2,*c1,*c2,*d1,*d2;
  126.  
  127.     a = new stringnode("A");
  128.     b1 = new stringnode("B1",a);
  129.     b2 = new stringnode("B2",a);
  130.     c1 = new stringnode("C1",b1);
  131.     c2 = new stringnode("C2",b1);
  132.     d1 = new stringnode("D1",b2);
  133.     d2 = new stringnode("D2",b2);
  134.  
  135.     //*****************************
  136.  
  137.  
  138.     printf("\n\n* Scan the tree using the \"A\" node:\n");
  139.     a->docmd(cmd_printmyself,CCF_EVERYWHERE);
  140.  
  141.     printf("\n\n* Scan the tree using the \"D2\" node:\n");
  142.     d2->docmd(cmd_printmyself,CCF_EVERYWHERE);
  143.  
  144.  
  145.     //*****************************
  146.  
  147.     printf("\n\n* Print \"D1\" and its ascendant nodes\n");
  148.     d1->docmd(cmd_printmyself,CCF_ASCENDANT);
  149.  
  150.     printf("\n\n* Print \"B2\" and its descendant nodes\n");
  151.     b2->docmd(cmd_printmyself,CCF_DESCENDANT);
  152.  
  153.     //*****************************
  154.  
  155.     printf("\n\n* Delete the \"B1\" node:\n");
  156.     delete b1;
  157.  
  158.     printf("\n\n* Delete the \"A\" node:\n");
  159.     delete a;    
  160. }
  161.  
  162.  
  163.